package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constants.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.domain.business.BusinessPoolDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolListDTO;
import com.qk.dto.business.UpdateBusinessDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author: zyx
 * @created: 2025-10-15-10:59
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto) {
        log.info("新增商机,参数：{}", dto);
        businessService.add(dto);
        return Result.success();
    }

    @GetMapping
    public Result page(BusinessListDTO dto) {
        PageResult<BusinessDO> result =businessService.selectPage(dto);
        log.info("分页查询结果：{}", result);
        return Result.success(result);
    }

    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .updateTime(LocalDateTime.now())
                .userId(userId)
                .status(BusinessStatusConstants.TO_BE_FOLLOW_UP)
                .build()
        );
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id) {
        BusinessVO vo =businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }

    @PutMapping
    public Result followUpBusiness(@RequestBody UpdateBusinessDTO dto) {
        businessService.followUpBusiness(dto);
        return Result.success();
    }

    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }

    @PutMapping("/back/{id}")
    public Result backToSea(@PathVariable Integer id) {
        businessService.backToSea(id);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result poolPage(BusinessPoolListDTO dto){
        PageResult<BusinessPoolDO> result =businessService.poolPage(dto);
        log.info("公海池分页查询结果：{}", result);
        return Result.success(result);
    }
}